#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from xpy3lib.XRetryableQuery import XRetryableQuery
from sicost.AbstractDPJob import AbstractDPJob
from sicost.DP011_M_Job import DP011_M_Job
from sicost.DP011_O_Job import DP011_O_Job
from sicost.DP011_L_Job import DP011_L_Job
from sicost.DP011_N_Job import DP011_N_Job
from sicost.DP011_Z_Job import DP011_Z_Job
from sicost.DP011_Z_A_Job import DP011_Z_A_Job
from sicost.DP011_Z_B_Job import DP011_Z_B_Job


class DP0110Job(AbstractDPJob):
    """
    获取消耗信息 按照类型调用不同存储过程

    SU_AJBG_DP0102 主原料
    BPC 能耗
    BPC 锌耗

    从main函数传入参数
    p_account,p_cost_center,p_unit,p_account_period_start,p_account_period_end,p_data_type
    """

    cost_center_org = None
    data_type = None

    # NOTE p_wce_org =v_wce,
    # NOTE p_wce=LEFT(v_wce,1)||'@@@@'
    wce_org = None
    wce = None

    def __init__(self,
                 p_config=None,
                 p_db_conn_mpp=None,
                 p_db_conn_rds=None,
                 p_db_conn_dbprod7=None,
                 p_unit=None,
                 p_account=None,
                 p_cost_center_org=None,
                 p_cost_center=None,
                 p_account_period_start=None,
                 p_account_period_end=None,
                 p_data_type=None,
                 p_wce_org=None,
                 p_wce=None,p_df7=None):
        """

        :param p_config:
        :param p_db_conn_mpp:
        :param p_db_conn_rds:
        :param p_db_conn_dbprod7:
        :param p_unit:
        :param p_account:
        :param p_cost_center_org:
        :param p_cost_center:
        :param p_account_period_start:
        :param p_account_period_end:
        :param p_data_type: data type是 0 D M。分别是 实时、每日、每月
        :param p_wce_org: 成本科目编号
        :param p_wce:
        """
        super(DP0110Job, self).__init__(p_config=p_config,
                                        p_db_conn_mpp=p_db_conn_mpp,
                                        p_db_conn_rds=p_db_conn_rds,
                                        p_db_conn_dbprod7=p_db_conn_dbprod7,
                                        p_unit=p_unit,
                                        p_account=p_account,
                                        p_cost_center=p_cost_center,
                                        p_account_period_start=p_account_period_start,
                                        p_account_period_end=p_account_period_end)
        self.cost_center_org = p_cost_center_org
        self.data_type = p_data_type
        self.wce_org = p_wce_org
        self.wce = p_wce
        self.df7 = p_df7

    def do_execute(self):
        """
        """
        self.logger.info('DP0110Job.do_execute')

        CAL_TYPE, COEF, MON = self.__step_1()

        # IF CAL_TYPE='M'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE
        # 调用DP011MJob

        # IF CAL_TYPE='P'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE
        # 调用DP011PJob

        # IF CAL_TYPE='tmp_l'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE，P_COEF=COEF
        # 调用DP011LJob

        # IF CAL_TYPE='N'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_CAL_TYPE=CAL_TYPE
        # 调用DP011NJob

        # IF CAL_TYPE='O' 这个是英文大写o不是数字0
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE，P_MON=MON
        # 调用DP011OJob 这个是英文大写o不是数字0

        # IF CAL_TYPE='Z'
        # SELECT CAL_TABLE_TYPE AS CAL_TYPE
        # FROM BGRAGGCB.WH_AJBG_DP0110
        # WHERE ACCOUNT = '1001'
        # AND COST_CENTER = 'MIDF'
        # AND UNIT='Q161'
        # AND SOURCE_NO='0'
        # AND WCE = '68300'
        # 其中1001,MIDF,Q161,0,68300是传入的参数
        # p_account,p_cost_center,p_unit,p_data_type,p_wce
        # 得到新的CAL_TYPE 要么是A要么是B
        # IF CAL_TYPE='A'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE,P_COEF=COEF
        # 调用DP011AJob
        #
        # IF CAL_TYPE='B'
        # 用参数
        # P_ACCOUNT,P_COST_CENTER_ORG,P_COST_CENTER,P_UNIT,P_ACCOUNT_PERIOD_START,P_ACCOUNT_PERIOD_END,P_WCE_ORG,P_WCE,P_DATA_TYPE
        # 调用DP011BJob

        self.__step_2(p_cal_type=CAL_TYPE, p_coef=COEF, p_mon=MON,p_df7=self.df7)

        super(DP0110Job, self).do_execute()

    def __step_2(self, p_cal_type=None, p_coef=None, p_mon=None,p_df7=None):
        if p_cal_type == 'M':
            DP011_M_Job(p_config=self.config,
                        p_db_conn_mpp=self.db_conn_mpp,
                        p_db_conn_rds=self.db_conn_rds,
                        p_db_conn_dbprod7=self.db_conn_dbprod7,
                        p_unit=self.unit,
                        p_account=self.account,
                        p_cost_center_org=self.cost_center,
                        p_cost_center=self.cost_center,
                        p_account_period_start=self.account_period_start,
                        p_account_period_end=self.account_period_end,
                        p_data_type=self.data_type,
                        p_wce_org=self.wce_org,
                        p_wce=self.wce,
                        p_cal_type=p_cal_type,
                        p_coef=p_coef,
                        p_mon=p_mon,p_df7=p_df7).execute()
        if p_cal_type == 'O':
            DP011_O_Job(p_config=self.config,
                        p_db_conn_mpp=self.db_conn_mpp,
                        p_db_conn_rds=self.db_conn_rds,
                        p_db_conn_dbprod7=self.db_conn_dbprod7,
                        p_unit=self.unit,
                        p_account=self.account,
                        p_cost_center_org=self.cost_center,
                        p_cost_center=self.cost_center,
                        p_account_period_start=self.account_period_start,
                        p_account_period_end=self.account_period_end,
                        p_data_type=self.data_type,
                        p_wce_org=self.wce_org,
                        p_wce=self.wce,
                        p_cal_type=p_cal_type,
                        p_coef=p_coef,
                        p_mon=p_mon,p_df7=p_df7).execute()
        if p_cal_type == 'tmp_l':
            DP011_L_Job(p_config=self.config,
                        p_db_conn_mpp=self.db_conn_mpp,
                        p_db_conn_rds=self.db_conn_rds,
                        p_db_conn_dbprod7=self.db_conn_dbprod7,
                        p_unit=self.unit,
                        p_account=self.account,
                        p_cost_center_org=self.cost_center,
                        p_cost_center=self.cost_center,
                        p_account_period_start=self.account_period_start,
                        p_account_period_end=self.account_period_end,
                        p_data_type=self.data_type,
                        p_wce_org=self.wce_org,
                        p_wce=self.wce,
                        p_cal_type=p_cal_type,
                        p_coef=p_coef,
                        p_mon=p_mon,p_df7=p_df7).execute()
        if p_cal_type == 'N':
            DP011_N_Job(p_config=self.config,
                       p_db_conn_mpp=self.db_conn_mpp,
                       p_db_conn_rds=self.db_conn_rds,
                       p_db_conn_dbprod7=self.db_conn_dbprod7,
                       p_unit=self.unit,
                       p_account=self.account,
                       p_cost_center_org=self.cost_center,
                       p_cost_center=self.cost_center,
                       p_account_period_start=self.account_period_start,
                       p_account_period_end=self.account_period_end,
                       p_data_type=self.data_type,
                       p_wce_org=self.wce_org,
                       p_wce=self.wce,
                       p_cal_type=p_cal_type,
                       p_coef=p_coef,
                       p_mon=p_mon,p_df7=p_df7).execute()
        if p_cal_type == 'NS':
            DP011_N_Job(p_config=self.config,
                       p_db_conn_mpp=self.db_conn_mpp,
                       p_db_conn_rds=self.db_conn_rds,
                       p_db_conn_dbprod7=self.db_conn_dbprod7,
                       p_unit=self.unit,
                       p_account=self.account,
                       p_cost_center_org=self.cost_center,
                       p_cost_center=self.cost_center,
                       p_account_period_start=self.account_period_start,
                       p_account_period_end=self.account_period_end,
                       p_data_type=self.data_type,
                       p_wce_org=self.wce_org,
                       p_wce=self.wce,
                       p_cal_type=p_cal_type,
                       p_coef=p_coef,
                       p_mon=p_mon,p_df7=p_df7).execute()
        if p_cal_type == 'Z':
            DP011_Z_Job(p_config=self.config,
                       p_db_conn_mpp=self.db_conn_mpp,
                       p_db_conn_rds=self.db_conn_rds,
                       p_db_conn_dbprod7=self.db_conn_dbprod7,
                       p_unit=self.unit,
                       p_account=self.account,
                       p_cost_center_org=self.cost_center,
                       p_cost_center=self.cost_center,
                       p_account_period_start=self.account_period_start,
                       p_account_period_end=self.account_period_end,
                       p_data_type=self.data_type,
                       p_wce_org=self.wce_org,
                       p_wce=self.wce,
                       p_cal_type=p_cal_type,
                       p_coef=p_coef,
                       p_mon=p_mon).execute()


    def __step_1(self):
        # FIXME 有优化的空间， 3个SQL合成1个
        # NOTE 子步骤1 读取出传入的WCE（成本科目）所对应的处理逻辑 CAL_TYPE . dbprod7
        p_app_throw_ai_mode = '%PN%'
        sql = " SELECT CALC_TYPE_CODE AS CAL_TYPE,COEFF AS COEF,ROLL_MONTH AS MON" \
              " FROM BGTARAS1.T_ADS_WH_SICB_DP0110" \
              " WHERE 1=1 " \
              " AND ACCT='%s'" \
              " AND COST_CENTER='%s'" \
              " AND UNIT_CODE='%s'" \
              " AND SOURCE_CODE='%s'" \
              " AND COST_SUBJECT=%s" % (self.account, self.cost_center, self.unit, self.data_type, self.wce)
        self.logger.info(sql)
        df = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        df.columns = df.columns.str.upper()
        self.logger.info(df)
        success = df.empty is False
        CAL_TYPE = df.loc[0]['CAL_TYPE'] if success else None
        COEF = df.loc[0]['COEF'] if success else None
        MON = df.loc[0]['MON'] if success else None
        # sql = " SELECT " \
        #       " COEFF AS COEF" \
        #       " FROM BGTARAS1.T_ADS_WH_SICB_DP0110" \
        #       " WHERE 1=1" \
        #       " AND ACCT = '%s'" \
        #       " AND COST_CENTER = '%s'" \
        #       " AND UNIT_CODE='%s'" \
        #       " AND SOURCE_CODE='%s'" \
        #       " AND COST_SUBJECT = %s" % (self.account, self.cost_center, self.unit, self.data_type, self.wce)
        # self.logger.info(sql)
        # df = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        # df.columns = df.columns.str.upper()
        # self.logger.info(df)
        # success = df.empty is False
        # COEF = df.loc[0]['COEF'] if success else None
        #
        # sql = " SELECT " \
        #       " ROLL_MONTH AS MON " \
        #       " FROM BGTARAS1.T_ADS_WH_SICB_DP0110" \
        #       " WHERE 1=1 " \
        #       " AND ACCT = '%s'" \
        #       " AND COST_CENTER = '%s'" \
        #       " AND UNIT_CODE='%s'" \
        #       " AND SOURCE_CODE='%s'" \
        #       " AND COST_SUBJECT = %s" % (self.account, self.cost_center, self.unit, self.data_type, self.wce)
        # self.logger.info(sql)
        # df = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        # df.columns = df.columns.str.upper()
        # self.logger.info(df)
        # success = df.empty is False
        # MON = df.loc[0]['MON'] if success else None

        return CAL_TYPE, COEF, MON
